<?php

class LiveController extends Controller
{
	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'ajaxOnly + getQuote',	//Only allow getQuote via AJAX request
			'postOnly + delete', // we only allow deletion via POST request
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow', // allow authenticated user to perform 'index' and 'view' actions
				'actions'=>array('index','view'),
				'users'=>array('@'),
			),
			array('deny',  // deny all users
					'users'=>array('*'),
			),
		);
	}

	/**
	 * Displays a particular model.
	 * @param integer $id the ID of the model to be displayed
	 */
	public function actionView($cid, $pid = null, $camId = null)
	{		
		if($cid===null)
			throw new CHttpException(404,'The requested page does not exist. Please make sure the cid exist.');
		
		$dataProvider=new CActiveDataProvider('Project', array(
				'criteria'=>array(
						'condition'=>'county_id=' . $cid,
						'order'=>'name',
				//		'with'=>array('author'),
				),
				'pagination'=>array(
						'pageSize'=>20,
				),
		));
		
		if($pid != null) {
			$this->render('view',array(
					'dataProvider'=>$dataProvider,
					'county'=>$this->loadCountyModel($cid),
					'project'=>$this->loadProjectModel($pid),
					'camId' => $camId,
			));
		} else {
			$this->render('view',array(
					'dataProvider'=>$dataProvider,
					'county'=>$this->loadCountyModel($cid),
					'project'=>null,
					'camId' => $camId,
			));
		}
		
	}
	
	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('County', array(
				'criteria'=>array(
				//		'condition'=>'county_id=' . $cid,
						'order'=>'name',
				//		'with'=>array('author'),
				),
				'pagination'=>array(
						'pageSize'=>20,
				),
		));
		
		$projects=new Project('search');
		$projects->unsetAttributes();  // clear any default values
		if(isset($_GET['Project']))
			$projects->attributes=$_GET['Project'];

		$this->render('index',array(
			'projects'=>$projects,
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return County the loaded model
	 * @throws CHttpException
	 */
	public function loadCountyModel($id)
	{
		$model=County::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return Project the loaded model
	 * @throws CHttpException
	 */
	public function loadProjectModel($id)
	{
		$model=Project::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}
